﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Text;
using System.Configuration;

namespace CheckList
{
    class classConfig
    {
        //połaczenie bezpośrednio z serwerem testowym ASA
        //public static string sqlConnString = @"DRIVER={Adaptive Server Anywhere 8.0};SERVER=kowalsylwester;DATABASE=test;UID=dba;PWD=sql";
        
        //połaczenie za posrednictwem odbc z serwerem orginalnym
        public static string sqlConnString = "DSN=produkcja;UID=dba;PWD=sql";

        //wersja testowa ODBC
        //public static string sqlConnString = "DSN=testDB;UID=dba;PWD=sql";

        public static string getConfSettingsForKey(string key)
        {
            string filetype = ConfigurationSettings.AppSettings[key];
            return filetype;
        }

        public static void exec(string queryString, string connectionString)
        {
            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                OdbcCommand command = new OdbcCommand(queryString, connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }

        public static string getOne(string queryString, string connectionString)
        {
            DataSet ds = new DataSet();
            OdbcConnection conn = new OdbcConnection(connectionString);
            conn.Open();

            OdbcDataAdapter xDataAdapter = new OdbcDataAdapter(queryString, conn);
            //Fills dataset with the records from database 
            xDataAdapter.Fill(ds, "one");
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count == 1)
            {
                DataRow dr = dt.Rows[0];
                return dr[0].ToString();
            }
            else
            {
                return "";
            }
        }

        public static DataRow getRow(string queryString, string connectionString)
        {
            DataSet ds = new DataSet();
            OdbcConnection conn = new OdbcConnection(connectionString);
            conn.Open();

            OdbcDataAdapter xDataAdapter = new OdbcDataAdapter(queryString, conn);
            //Fills dataset with the records from database 
            xDataAdapter.Fill(ds, "one");
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count == 1)
            {
                DataRow dr = dt.Rows[0];
                return dr;
            }
            else
            {
                return null;
            }
        }

        public static DataTable getDTable(string queryString, string connectionString)
        {
            DataSet ds = new DataSet();
            OdbcConnection conn = new OdbcConnection(connectionString);
            conn.Open();

            OdbcDataAdapter xDataAdapter = new OdbcDataAdapter(queryString, conn);
            //Fills dataset with the records from database 
            xDataAdapter.Fill(ds, "one");
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count >= 1)
            {
                return dt;
            }
            else
            {
                return null;
            }
        }

        public static DataSet getDSet(string queryString, string connectionString)
        {
            DataSet ds = new DataSet();
            OdbcConnection conn = new OdbcConnection(connectionString);
            conn.Open();

            OdbcDataAdapter xDataAdapter = new OdbcDataAdapter(queryString, conn);
            //Fills dataset with the records from database 
            xDataAdapter.Fill(ds, "ds");
            conn.Close();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count >= 1)
            {
                return ds;
            }
            else
            {
                return null;
            }
        }
    }
}
